from datetime import datetime, timedelta, timezone
import pytest
from common.request_util_test import RequestUtil
from common.test_yaml_util_test import YamlUtil
from config import datas


class Testalert:
    url_unify = "/clusters/host"
    starttime = datetime.now(tz=timezone.utc).strftime('%Y-%m-%dT%H:%M:%SZ')
    endtime = (datetime.now(tz=timezone.utc) + timedelta(days=1)).strftime('%Y-%m-%dT%H:%M:%SZ')

    @pytest.mark.normal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v25.06/alertmanager/1_get_alert_list.yaml'))
    def test_get_alert_list(self, alertinfo):
        # 获取alert列表接口-正常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/1_get_alert_list_exception.yaml'))
    def test_get_alert_list_exception(self, alertinfo):
        # 获取alert列表接口-异常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/2_create_silence_rule.yaml'))
    def test_create_silence_rule(self, alertinfo):
        # 创建静默规则-正常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']
        alertinfo['request']['data']['startsAt'] = Testalert.starttime
        alertinfo['request']['data']['endsAt'] = Testalert.endtime
        data = alertinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        globals()['silenceId'] = req.json()['silenceID']
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/2_create_silence_rule_exception.yaml'))
    def test_create_silence_rule_exception(self, alertinfo):
        # 创建静默规则-异常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']

        if alertinfo['request']['data']['startsAt'] != "{starttime}1":
            alertinfo['request']['data']['startsAt'] = Testalert.starttime
        else:
            alertinfo['request']['data']['startsAt'] = Testalert.starttime + "1"
        if alertinfo['request']['data']['endsAt'] != "{endtime}1":
            alertinfo['request']['data']['endsAt'] = Testalert.endtime
        else:
            alertinfo['request']['data']['startsAt'] = Testalert.endtime + "1"
        data = alertinfo['request']['data']
        req = RequestUtil().send_request(method, url, json=data)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo', YamlUtil().read_testcase_yaml('v25.06/alertmanager/3_get_silence_list.yaml'))
    def test_get_silence_list(self, alertinfo):
        # 获取silence列表接口-正常
        base_url = datas.baseUrl + Testalert.url_unify
        if '{silenceId}' in alertinfo['request']['url']:
            url = base_url + alertinfo['request']['url'].replace('{silenceId}', globals()['silenceId'])
        else:
            url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/3_get_silence_list_exception.yaml'))
    def test_get_silence_list_exception(self, alertinfo):
        # 获取silence列表接口-异常
        base_url = datas.baseUrl + Testalert.url_unify
        if '{silenceId}' in alertinfo['request']['url']:
            url = base_url + alertinfo['request']['url'].replace('{silenceId}', globals()['silenceId'])
        else:
            url = base_url + alertinfo['request']['url']
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.normal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/4_delete_silence_rule.yaml'))
    def test_delete_silence_rule(self, alertinfo):
        # 删除静默规则-正常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url'].replace('{silenceId}', globals()['silenceId'])
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']

    @pytest.mark.abnormal
    @pytest.mark.alertmanager
    @pytest.mark.parametrize('alertinfo',
                             YamlUtil().read_testcase_yaml('v25.06/alertmanager/4_delete_silence_rule_exception.yaml'))
    def test_delete_silence_rule_exception(self, alertinfo):
        # 删除静默规则-异常
        base_url = datas.baseUrl + Testalert.url_unify
        url = base_url + alertinfo['request']['url'].replace('{silenceId}', globals()['silenceId'])
        method = alertinfo['request']['method']
        req = RequestUtil().send_request(method, url)
        assert req.status_code == alertinfo['validate'][0]['equals']['status_code']
